package com.zhen.aikan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhen.aikan.entity.VodType;
import com.zhen.aikan.mapper.VodTypeMapper;
import com.zhen.aikan.service.IVodTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhen.aikan.vo.TypeVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lijiazhen
 * @since 2023-09-11
 */
@Service
public class VodTypeServiceImpl extends ServiceImpl<VodTypeMapper, VodType> implements IVodTypeService {

    @Override
    public List<VodType> getLikeName(String vodType) {
        return baseMapper.selectList(new QueryWrapper<VodType>().like("type_name", vodType));
    }

    @Override
    public List<TypeVo> getAllType() {
        List<VodType> vodParentTypes = baseMapper.selectList(new QueryWrapper<VodType>().eq("type_pid",0));
        List<TypeVo> parents = new ArrayList<>();
        vodParentTypes.forEach(vt -> {
            TypeVo parent = new TypeVo();
            BeanUtil.copyProperties(vt,parent);
            List<VodType> vodSonTypes = baseMapper.selectList(new QueryWrapper<VodType>().eq("type_pid", vt.getTypeId()));
            List<TypeVo> son = BeanUtil.copyToList(vodSonTypes, TypeVo.class);
            parent.setTypeSons(son);
            parents.add(parent);
        });
        return parents;
    }

    @Override
    public Integer getTypeIdByTypeName(String type) {
        return baseMapper.selectOne(new QueryWrapper<VodType>().eq("type_name", type)).getTypeId();
    }
}
